package com.dp.appi.dao.impl;

import com.dp.appi.dao.OauthDao;
import com.dp.appi.util.PasswordUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by xiaoji on 2016/4/20.
 * 认证数据层
 */
@Component
public class OauthDaoImpl extends BaseDaoImpl implements OauthDao {
    /**
     * 注册
     *
     * @param username    用户名
     * @param mobilePhone 手机号码
     * @param pwd
     */
    @Override
    public void insertUser(String username, String mobilePhone, String pwd) {
        String id = UUID.randomUUID().toString().replace("-","");
        //插入 t_s_base_user
        String sql2 = "insert into t_s_base_user(ID,realname,username,password,status,userkey,departid,usertype) values(?,?,?,?,1,'普通用户','297e640454cbba690154cd8eb0460010',12)";
        getJdbcTemplate().update(sql2,new Object[]{id,username,username,
                PasswordUtil.encrypt(username,pwd,PasswordUtil.getStaticSalt())});
        //插入 t_s_user
        String sql1 = "insert into t_s_user(id,mobilePhone) values(?,?)";
        getJdbcTemplate().update(sql1,new Object[]{id,mobilePhone});

        //插入 t_s_role_org 加入散户集散中心
        String sql3 = "INSERT INTO `t_s_user_org` (`ID`, `user_id`, `org_id`) VALUES (?, ?, '297e640454cbba690154cd8eb0460010');";
        getJdbcTemplate().update(sql3,new Object[]{UUID.randomUUID().toString().replace("-",""),id});

        //加入散户使用角色
        String sql4 = "INSERT INTO `t_s_role_user` (`ID`, `roleid`, `userid`) VALUES (?, ?, ?);";
        getJdbcTemplate().update(sql4,new Object[]{UUID.randomUUID().toString().replace("-",""),"ff80808155bef7320155bf3c3d9b0016",id});
    }

    /**
     * 用户账号是否存在
     *
     * @param account 账号
     * @return
     */
    @Override
    public boolean isExsitUsername(String account) {
        String sql = "select * from t_s_base_user tsb inner join t_s_user tsu on tsb.ID=tsu.id" +
                " where tsb.username = ? or tsu.mobilePhone=?";
        List<Map<String,Object>> list = getJdbcTemplate().queryForList(sql,new Object[]{account,account});
        if(list.size() > 0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 根据账号获取用户对象
     *
     * @param username 账号
     * @return
     */
    @Override
    public Map<String, Object> getUser(String username) {
        String sql = "select * from t_s_base_user tsb inner join t_s_user tsu on tsb.ID=tsu.id" +
                " where tsb.username = ? or tsu.mobilePhone=? ";
        List<Map<String,Object>> list = getJdbcTemplate().query(sql,new Object[]{username,username},getColumnMapRowMapper());
        if(list.size() == 1){
                return list.get(0);
        }else{
            return null;
        }
    }

    /**
     * 重设密码
     *
     * @param id 用户id
     * @param newpwd  新密码
     */
    @Override
    public void resetPwd(String id, String newpwd) {
        String sql="update t_s_base_user set password=? where ID=?";
        getJdbcTemplate().update(sql,new Object[]{newpwd,id});
    }

    /**
     * 获取密保问题
     *
     * @param id 账号
     * @return
     */
    @Override
    public Map<String, Object> getSecurityProblem(String id) {
        String sql = "select security_problem1,security_problem2 from t_s_user where id=?";
        return findOneObject(sql,new Object[]{id});
    }

    /**
     * 修改密码
     *
     * @param id 手机号码
     * @param pwd   密码
     */
    @Override
    public void updatePwd(String id, String pwd) {
        String sql="update t_s_base_user set password=? where ID=?";
        getJdbcTemplate().update(sql,new Object[]{pwd,id});
    }

    /**
     * 设置密保答案
     *
     * @param id       登录id
     * @param problem1 问题1
     * @param answer1  答案1
     * @param problem2 问题2
     * @param answer2  答案2
     */
    @Override
    public void setSecurityProblem(String id, String problem1, String answer1, String problem2, String answer2) {
        String sql="update t_s_user set security_problem1=?,security_answer1=?,security_problem2=?,security_answer2=? where id=?";
        getJdbcTemplate().update(sql,new Object[]{problem1,answer1,problem2,answer2,id});
    }
}
